//
//  FlashSalceCollectionViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/25.
//

import UIKit


class FlashSalceCollectionViewCell: UICollectionViewCell {
    var bgView: UIView!
    var imgView: UIImageView!
    var sellOutView: UIView!
    var sellOutImageView: UIImageView!
    var textView: UITextView!//标题
    var priceLab: UILabel!//售价
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        
        self.bgView = UIView.init(frame: CGRect.zero)
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.layer.masksToBounds = true
        self.contentView.addSubview(self.bgView)
        
        imgView = UIImageView.init(frame: CGRect.zero)
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        self.bgView.addSubview(imgView)
        
        
        self.sellOutView = UIView.init(frame: CGRect.zero)
        self.sellOutView.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.2)
        self.imgView.addSubview(self.sellOutView)
        
        
        self.sellOutImageView = UIImageView.init(frame: CGRect.zero)
        self.sellOutImageView.layer.contentsGravity = kCAGravityResizeAspectFill//防止变形
        self.sellOutView.addSubview(self.sellOutImageView)
        
        
        
        self.textView = UITextView.init(frame: CGRect.init())
        self.textView.backgroundColor = .clear
        self.textView.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.textView.textColor = Theme.titleLabColor
        self.textView.isUserInteractionEnabled = false
        self.textView.textContainer.maximumNumberOfLines = 2
        self.textView.textContainer.lineBreakMode = .byClipping
        self.textView.textContainer.lineFragmentPadding = .zero
        self.contentView.addSubview(self.textView)
        
        self.priceLab = UILabel.init(frame: CGRect.init())
        self.priceLab.backgroundColor = .clear
        self.priceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.priceLab.textColor = Theme.mainColor
        self.contentView.addSubview(self.priceLab)
        

  
        
        self.addLayout()
    }
    
    

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addLayout(){
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(102)
        }
        
        
//        let tempH: CGFloat = (kScreenWidth - 36)/2 * (3/4)
        self.imgView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(102)
        }
        
        
        self.sellOutView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.width.equalTo(self.imgView.snp.width)
            make.height.equalTo(self.imgView.snp.height)
            make.left.equalTo(0)
        }
     
        
        self.sellOutImageView.snp.makeConstraints { (make) in
            make.width.equalTo(70)
            make.height.equalTo(70)
            make.centerY.equalTo(self.sellOutView.snp.centerY)
            make.centerX.equalTo(self.sellOutView.snp.centerX)
        }
        
        
        self.textView.snp.makeConstraints { make in
            make.top.equalTo(imgView.snp.bottom).offset(2)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
        }
        
        
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(self.textView.snp.bottom).offset(0)
            make.left.equalTo(0)
            make.height.greaterThanOrEqualTo(20).priorityHigh()
            make.right.equalTo(0)
//            make.bottom.equalTo(-10)
        }
        
    }
    
    

   
    
    
    
    
    //限时抢购
    func setProductsModel(productsModel: ProductsDataModel){
        if let url = URL.init(string: productsModel.productImage ?? "") {
            self.imgView.sd_setImage(with: url, placeholderImage: UIImage(named: "icon_default_place"))
        }else{
            self.imgView.image = UIImage(named: "icon_default_place")
        }
        self.textView.text = productsModel.name ?? ""
        let price = XTManageCenter.showPriceInComma(source: (productsModel.promotionPrice ?? 0).cleanZero)
        let text = NSMutableAttributedString(string: String(format: "%@%@", XTManageCenter.currentSign(),price))
        text.addAttribute(NSAttributedStringKey.font, value: UIFont.init(name: Theme.PingFangSCRegular, size: 11)!, range: NSMakeRange(0, (XTManageCenter.currentSign().count)))
        self.priceLab.attributedText = text
        

        self.sellOutView.isHidden = true
        if (productsModel.availableStock ?? 0) > 0 {
            self.sellOutView.isHidden = true
        }else{ //抢光了
            self.sellOutView.isHidden = false
//            let language = XTManageCenter.getLocalStringDataValue(aKey: XTManageCenter.kAppLanguage)
//            if language.contains("en"){//英文
//                self.sellOutImageView.image = UIImage(named: "icon_sold_out_en")
//            }else{
//                self.sellOutImageView.image = UIImage(named: "icon_sold_out")
//            }
            
        }
        self.updateConstraints()
        
    }

}
